package com.tujh.android.designpatterns.FactoryMethod.SimpleFactory;

/**
 * Created by TUJH on 2016/10/21.
 */
public class SendFactory {

    /**
     * 简单工厂模式
     *
     * @param type
     * @return
     */
    public Sender produce(String type) {
        if ("mail".equals(type)) {
            return new MailSender();
        } else if ("sms".equals(type)) {
            return new SmsSender();
        } else {
            System.out.println("请输入正确的类型!");
            return null;
        }
    }

    /**
     * 简单工厂模式(反射方法改进)
     *
     * @param className
     * @return
     */
    public Sender produceByClass(String className) {
        try {
            return (Sender) Class.forName(className).newInstance();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        return null;
    }

    //------------------------多个工厂方法模式----------------------------
    public Sender produceMail() {

        return new MailSender();
    }

    public Sender produceSms() {
        return new SmsSender();
    }

    //------------------------静态工厂方法模式----------------------------
    public static Sender produceMailStatic() {
        return new MailSender();
    }

    public static Sender produceSmsStatic() {
        return new SmsSender();
    }

}
